package com.example.edu.domain;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yanzhao
 * @date 2022/1/15 18:45
 * @since 1.0.0
 */
@Data
public class Result<T> {

    private String code;

    private String msg;

    private Boolean success;

    private Long t = System.currentTimeMillis();

    private Object data;

    public Result() {
    }

    public static Result success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return success(data, StatusCode.SUCCESS_CODE);
    }

    public static <T> Result<T> successWrapper(T data) {
        return successWrapper(data, StatusCode.SUCCESS_CODE);
    }

    public static <T> Result<T> successWrapper(T data, String code) {
        Map<String, Object> r = new HashMap<>();
        r.put("data", data);
        return success(r, code);
    }

    public static <T> Result<T> success(Object data, String code) {
        Result<T> result = new Result();
        result.setSuccess(true);
        result.setData(data);
        result.setCode(code);
        return result;
    }

    public static <T> Result<T> error(String message) {
        return error(message, StatusCode.FAILURE_CODE);
    }

    public static <T> Result<T> error(String message, String code) {
        Result<T> result = new Result();
        result.setSuccess(false);
        result.setMsg(message);
        result.setCode(code);
        return result;
    }

    public interface StatusCode {
        String SUCCESS_CODE = "200";
        String FAILURE_CODE = "500";
        String PARAM_MISSING = "10001";
        String DUPLICATE_KEY = "10002";
        String NO_SUCH_MESSAGE_KEY = "10003";
    }
}
